# Replication archive for: Alexander Coppock, Kimberly Gross, Ethan Porter, Emily Thorson, and Thomas J. Wood, 
# "Conceptual Replication of Four Key Findings about Factual Corrections and Misinformation during the 2020 US Election: Evidence from Panel-Survey Experiments." 
# Forthcoming in the British Journal of Political Science doi:10.1017/S0007123422000631

library(tidyverse)
library(magrittr)
library(plotrix)

all_panels_long <- read_rds("data/all_panels_long.rds")
treatments_df <- read_rds("data/treatments_df.rds")

t0 <- all_panels_long
t1 <- treatments_df


t2 <- t0 %>% 
  group_by(
    congeniality, lucid_pid_3, treatment, topic_short_2
  ) %>% 
  summarize(
    mu = outcome_w1 %>% mean(na.rm = T),
    se = outcome_w1 %>% plotrix::std.error(na.rm = T)
  ) %>% 
  mutate(
    lo = mu %>% 
      subtract(
        se %>% multiply_by(1.96)
      ),
    hi = mu %>% 
      add(
        se %>% multiply_by(1.96)
      )
  ) %>% 
  filter(
    treatment %>% 
      equals("control") &
    lucid_pid_3 %>% 
      is_in(
        c("Democrat",
          "Republican")
        )
    ) %>% 
  mutate(
    cfac = "Agreement in control"
  ) %>% 
  bind_rows(
    t1 %>% 
      select(
        topic_short_2, congeniality, traffic
      ) %>% 
      mutate(
        cfac = "Fact check website traffic"
      )  
  ) %>% 
  left_join(
    t1 %>% 
      select(
        topic_short_2, date_fielded
      ) %>% 
      mutate(
        ts2 = topic_short_2 %>% 
          str_c(
            " (",
            date_fielded %>% 
              mday,
            " ",
            date_fielded %>% 
              month(label = T, abbr = T),
            ")"
          )
      ) %>% 
      select(-date_fielded)
  ) %>% 
  mutate(
    cfac = cfac %>% 
      factor(
        c("Fact check website traffic",
          "Agreement in control")
      ) ,
    congeniality = congeniality %>% 
      str_wrap(
        width = 15
      ) %>% 
      factor(
        c("False claim\ncongenial to\nRepublicans",
          "False claim\ncongenial to\nDemocrats")
      )
    )

t2$ts2 %<>%
  factor(
    t2 %>% 
      filter(
        traffic %>% 
          is.na %>% 
          not
        ) %>% 
      group_by(ts2) %>% 
      summarize(traf = traffic %>% max) %>% 
      arrange(traf) %>% 
      use_series(ts2)
    )

t2$cfac %<>% 
  fct_rev

ggplot() +
  geom_col(
    aes(
      traffic, ts2  
    ),
    data = t2 %>% 
      filter(
        cfac %>% 
          str_detect("traffic")
      ),
    fill = "grey98",
    color = "grey2",
    size = .2,
    width = .55
  ) +
  geom_linerange(
    aes(
      xmin = lo,
      xmax = hi,
      y = ts2,
      group = lucid_pid_3
    ),
    data = t2 %>% 
      filter(
        cfac %>% 
          str_detect("traffic", negate = T)
        
      ),
    position = position_dodge(width = .1),
    linewidth = .3
  ) +
  geom_point(
    aes(
      mu, ts2,  fill = lucid_pid_3 
    ),
    shape = 21,
    size = 2.5,
    data = t2 %>% 
      filter(
        cfac %>% 
          str_detect("traffic", negate = T)
        ),
    position = position_dodge(width = .1)
  ) +
  geom_text(
    aes(
      x, y, label = lab
    ),
    hjust = 0,
    size = 2.7,
    lineheight = .7,
    fontface = "italic",
    data = tribble(
      ~x, ~y, ~lab,
      45, t2$ts2 %>% levels %>% extract2(24),  "Republicans",
      20.5, t2$ts2 %>% levels %>% extract2(24),  "Democrats"
      ) %>%
      mutate(
        y = y %>%
          factor(t2$ts2 %>% levels),
        cfac = t2$cfac %>%
          levels %>%
          extract2(1) %>%
          factor(
            t2$cfac %>%
              levels
            ),
        congeniality = t2$congeniality %>%
          levels %>%
          extract2(1) %>%
          factor(
            t2$congeniality %>%
              levels
          )
      )
  ) +
  facet_grid(
    congeniality ~ cfac,
    scales = "free",
    space = "free_y", 
    drop = T
  ) 
